import pygame
from alien import Alien
import game_functions as gf
from settings import Settings
from ship import Ship
from pygame.sprite import Group #一个类
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

def run_game():
    pygame.init()                                                  #初始化，让pygame能够正确的工作
    ai_settings = Settings()                                       #声明一个方法使用Setting
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))     #调用pygame方法设置一个1200*800的屏幕
    pygame.display.set_caption("Aliens Invasion")                   #调用方法设置名字
    
    #创建Play按钮
    play_button = Button(ai_settings, screen, "Play")

    #创建一个用于存储游戏信息的实例
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    
    """创建一艘飞船"""
    ship = Ship(ai_settings, screen)
    
    """创建一个用于存储子弹的数组"""
    bullets = Group()

    """创建一个用于存储外星人的数组"""
    aliens = Group()

    """创建一个外星人"""
    alien = Alien(ai_settings,screen)

    """创建外星人群"""
    gf.create_fleet(ai_settings, screen, ship, aliens)

    while True:   #游戏的主循环
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
            aliens, bullets)   #gamefunction检测按键将值传给ship.Py
        if stats.game_active:    
            ship.update()           #ship.date()根据True/False来判断是否移动
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
                 bullets)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
            play_button)

run_game()